<template>
  <el-dialog
    :title="!dataForm.newLogId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="新房id" prop="newId">
      <el-input v-model="dataForm.newId" placeholder="新房id"></el-input>
    </el-form-item>
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="newStartTime">
      <el-input v-model="dataForm.newStartTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="到期时间" prop="newEndTime">
      <el-input v-model="dataForm.newEndTime" placeholder="到期时间"></el-input>
    </el-form-item>
    <el-form-item label="是否有效（0.无 1.有）" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="是否有效（0.无 1.有）"></el-input>
    </el-form-item>
    <el-form-item label="支付金额" prop="price">
      <el-input v-model="dataForm.price" placeholder="支付金额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          newLogId: 0,
          newId: '',
          userId: '',
          newStartTime: '',
          newEndTime: '',
          flag: '',
          price: ''
        },
        dataRule: {
          newId: [
            { required: true, message: '新房id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          newStartTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          newEndTime: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '是否有效（0.无 1.有）不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '支付金额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.newLogId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.newLogId) {
            this.$http({
              url: this.$http.adornUrl(`/house/newlog/info/${this.dataForm.newLogId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.newId = data.newLog.newId
                this.dataForm.userId = data.newLog.userId
                this.dataForm.newStartTime = data.newLog.newStartTime
                this.dataForm.newEndTime = data.newLog.newEndTime
                this.dataForm.flag = data.newLog.flag
                this.dataForm.price = data.newLog.price
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/newlog/${!this.dataForm.newLogId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'newLogId': this.dataForm.newLogId || undefined,
                'newId': this.dataForm.newId,
                'userId': this.dataForm.userId,
                'newStartTime': this.dataForm.newStartTime,
                'newEndTime': this.dataForm.newEndTime,
                'flag': this.dataForm.flag,
                'price': this.dataForm.price
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
